package catalog

import (
	"math/rand"
)

type CatalogService struct {
}

func NewCatalogService() *CatalogService {
	return &CatalogService{}
}

var itemNames = []string{
	"Code Smell",
	"Control Flow",
	"Interpreter",
	"Recursion",
	"Sprint",
	"Heisenbug",
	"Spaghetti",
	"Hydra Code",
	"Off-By-One",
	"Scope",
	"Callback",
	"Closure",
	"Automata",
	"Bit Shift",
	"Currying",
	"Cyclomatic Complexity",
	"Data Structure",
	"Debugging",
	"Debugger",
	"Decorator",
	"Dependency Injection",
	"Design Pattern",
	"Domain Model",
	"Encapsulation",
	"Exception",
	"Factory",
	"Facade",
	"Fluent Interface",
	"Functional Programming",
	"Garbage Collection",
	"Goto",
	"Grammar",
	"Hash Table",
	"Hashing",
	"Hot Code",
	"Hypertext Markup Language",
}

func (t *CatalogService) GetCatalog(page int, pageSize int) ([]CatalogModel, error) {
	count := len(itemNames)

    if page < 1 {
        page = 1
    }
    index := (page - 1) * pageSize
    if index > count {
        return make([]CatalogModel, 0), nil
    }
    if index + pageSize > count {
        pageSize = count - index
    }

	datas := make([]CatalogModel, pageSize)
    for i := 0; i < pageSize; i++ {
        datas[i] = CatalogModel{
            Id:   int32(index + i),
            Name: itemNames[index + i],
			Price: int32((index + i) * 10),
        }
    }
	rand.Shuffle(len(datas), func(i, j int) {
		datas[i], datas[j] = datas[j], datas[i]
	})
	return datas, nil
}
